package com.example.demo.controller;


import com.example.demo.config.ChatServiceException;
import com.example.demo.services.MongoService;
import com.example.demo.services.RedisService;
import com.example.demo.utils.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/friend")
public class FriendController {

    private static final Logger logger = LoggerFactory.getLogger(FriendController.class);

    @Autowired
    private RedisService chatService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MongoService mongoService;

    @PostMapping("/delete-friend/{userId}/{friendId}")
//    删除好友
    public Map<String, Object> deleteFriend(@PathVariable("userId") String userId, @PathVariable("friendId") String friendId) throws ChatServiceException {
        ApiResponse apiResponse = new ApiResponse();
        try {
            chatService.deleteFriend(userId, friendId);
            return ApiResponse.messageResultWithData("111", HttpStatus.OK, Arrays.asList(1, 2, 3));
        } catch (Exception e) {
            logger.info(e.getMessage());
            return ApiResponse.messageResult("22", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //    获取朋友列表（好友、群聊）
    @GetMapping("/get-chat-friend-list/{userId}")
    public Map<String, Object> getUserChatFriendList(@PathVariable("userId") String userId) {
//        获取好友列表
        List<Object> friendList = redisService.getFriendListFromRedis(userId);
        List<String> groupList = mongoService.getMembersInGroup(userId);

        Map<String, Object> chatList = Map.of("friendList", friendList, "groupList", groupList);
        Map<String, Object> result = new HashMap<>();
        result.put("data", chatList);
        return result;
    }


}
